<?php
// 房型管理
class HouseAction extends CommonAction {
    function _filter(&$map) {
       $key = trim($_REQUEST['keywords']);
         if(!empty($key)) {
             $map['name'] = array('like',"%".$key."%");
             $hotel_id = M('Hotel')->where("name like '%".$key."%'")->field('id')->findAll();
             $hotels = array();
             foreach ($hotel_id as $key => $value) {
                 $hotels[$key] = $value['id'];
             }
             $hotel_id = implode(",", $hotels);
             if(!empty($hotel_id)) {
                 $map['hotel_id'] = array('in',$hotel_id);
             }
             $map['_logic'] = "or";
         }
    }

    function _list($model, $map, $sortBy = '', $asc = false){
        parent::_list($model, $map, $sortBy = 'id', $asc = true);
    }

    function _before_add() {
        $this->assign('hotel',M('Hotel')->where('status=1')->findAll() );
        $this->assign('layout',C('layout') );
    }

    function _before_edit() {
        $this->assign('hotel',M('Hotel')->where('status=1')->findAll() );
        //$this->assign('price',D('Price')->getPrice($_REQUEST['id']) );
        $this->assign('layout',C('layout') );
        $Attach = M('Attach');
        $logo = $Attach ->where('house_id ='.$_REQUEST['id'].' and attachfunc = 2')->field('id,filename,path')->find();
		if(!empty($logo['filename'])){
			$this -> assign('logo_photo',__ROOT__.substr($logo['path'],1).'thumb_'.$logo['filename']);
		}
        $attach_photo = $Attach ->where('house_id ='.$_REQUEST['id'].' and attachfunc = 1')->order('sort')->findAll();
        $this -> assign('attach_photo',$attach_photo);
    }

    function insert() {
        $House = D('House');
        if(false == $House->create()) {
            $this->error($House->getError());
        }
        if(false !== $list = $House->add()) {
           // $this->_process_price($list);
            $this->process_photo($list,1);
            $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
            $this->success('房型添加成功');
        }
        else {
            $this->error('新增房型失败！');
        }
    }

    public function update(){
        $id = intval($_POST['id']);
        if($id <= 0){
            $this->error('编辑的产品不合法！');
        }
        $House = D('House');
        if(false == $House->create()) {
            $this->error($House->getError());
        }
        if(false !== $list = $House->save()) {
           // $this->_process_price($id);
            $this->process_photo($id,1);
            $this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
            $this->success('房型修改成功');
        }
        else {
            $this->error('房型修改失败！');
        }
    }

    protected function _process_price($list) {
        if(!is_numeric($_POST['default_price'])||$_POST['default_price']<=0 ) {
            $this->error('请指定默认价格');
        }
        $data = array(
            'house_id' => $list,
            'default' => $_POST['default_price'],
            'start_day' => $_POST['start_day'],
            'end_day' => $_POST['end_day'],
            'value' => $_POST['price'],
        );
        $price = D('Price');
        $status = $price ->addPrice($data);
        if(!status){
            $this->error('房型的价格插入错误！');
        }
        else{
            return;
        }
    }

   
}
?>
